//
//  SKXChatViewController.m
//  NewChat
//
//  Created by songKX on 16/3/7.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "SKXChatViewController.h"
#import "SendTableViewCell.h"
#import "ReceiveTableViewCell.h"
#import "SKXCallViewController.h"
@interface SKXChatViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,EMChatManagerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,EMCallManagerDelegate>

@property (nonatomic ,strong) NSMutableArray *messageDataArray;
@property (nonatomic , assign) CGFloat cellHeight;
@property (weak, nonatomic) IBOutlet UITableView *skxChatTableView;
@property (weak, nonatomic) IBOutlet UITextView *skxInputView;
@property (weak, nonatomic) IBOutlet UIButton *skxVoiceBtn;
@property (weak, nonatomic) IBOutlet UIView *skxToolsView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *skxToolsViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *skxToolsViewBottomSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *skxChatTableViewBottom;
@property (nonatomic , assign) BOOL keyBStatus;
@property (nonatomic , strong)UIView *skxMoreToolView;
@property (nonatomic , strong)UIButton *skxCallButton;
@property (nonatomic , strong)UIButton *skxImageButton;
@property (nonatomic , strong)UIButton *skxLocationButton;
@property (nonatomic , strong)EMCallSession *currentCall;
@property (nonatomic , copy)NSString *currentTime;
@end


@implementation SKXChatViewController

#pragma mark -----视图操作------

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.translucent = YES;
    
    self.edgesForExtendedLayout = UIRectEdgeTop|UIRectEdgeBottom;
    
    self.chaterName = self.chater.username;
    
    
    
    //添加代理
    self.skxInputView.delegate = self;
    
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    [[EaseMob sharedInstance].callManager addDelegate:self delegateQueue:nil];
    
    
    ////
    self.skxChatTableView.delegate = self;
    
    self.skxChatTableView.dataSource = self;
    ////
    //添加键盘的监听
    //监听键盘的弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KBWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //监听键盘消失
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KBWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    //添加聊天记录
    [self loadMessageHistory];
    
    //初始化skxMoreToolView
    
    _skxMoreToolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    _skxMoreToolView.backgroundColor = [UIColor greenColor];
    _skxMoreToolView.hidden = YES;
    [self.view addSubview:_skxMoreToolView];
    _skxMoreToolView.sd_layout
    .bottomSpaceToView(self.view,0)
    .leftSpaceToView(self.view,0)
    .rightSpaceToView(self.view,0)
    .heightIs(0);
    
    
    //button初始化
    self.skxCallButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    
    [self.skxCallButton setImage:[UIImage imageNamed:@"chatBar_colorMore_audioCall"] forState:UIControlStateNormal];
    
    [self.skxMoreToolView addSubview:self.skxCallButton];
    
    self.skxImageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    
    [self.skxImageButton setImage:[UIImage imageNamed:@"chatBar_colorMore_photo"] forState:UIControlStateNormal];
    
    [self.skxMoreToolView addSubview:self.skxImageButton];
    
    [self.skxChatTableView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelKeyBoard)]];
    
    
    //button布局
    self.skxCallButton.sd_layout
    .leftSpaceToView(self.skxMoreToolView,40)
    .topSpaceToView(self.skxMoreToolView,20)
    .widthIs(60)
    .heightIs(60);
    
    
    
    self.skxImageButton.sd_layout
    .topSpaceToView(self.skxMoreToolView,20)
    .centerXEqualToView(self.skxMoreToolView)
    .widthIs(60)
    .heightIs(60);
    
    //button添加事件
    
    [self.skxCallButton addTarget:self action:@selector(skxTurnToCallView) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.skxImageButton addTarget:self action:@selector(skxChooseImageToSend) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(removeMessageHistory)];
    
    
    [self.skxChatTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tiCell"];
    
}

-(void)cancelKeyBoard{
    
    
    if (self.keyBStatus == NO) {
        
        self.skxToolsViewBottomSpace.constant = 0;
        
        self.skxChatTableViewBottom.constant = 50;
        
        [UIView animateWithDuration:0.25 animations:^{
            
            
            [self.view layoutIfNeeded];
            
        }];
        
        self.skxMoreToolView.hidden = YES;
        
    }else{
        
        self.skxMoreToolView.hidden = YES;
        
        [self.skxInputView resignFirstResponder];
        
    }
    
}

-(void)removeMessageHistory{
    
    EMConversation * conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:self.chaterName conversationType:eConversationTypeChat];
    
    [conversation removeAllMessages];
    
    [self.messageDataArray removeAllObjects];
    
    [self.skxChatTableView reloadData];
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
    [[EaseMob sharedInstance].callManager addDelegate:self delegateQueue:nil];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = NO;
    
    [[EaseMob sharedInstance].callManager removeDelegate:self];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messageDataArray.count -1 inSection:0];
    
    [self.skxChatTableView reloadData];
    
    if (self.messageDataArray.count == 0) {
        
    }else{
        
        [self.skxChatTableView scrollToRowAtIndexPath:lastIndex atScrollPosition:(UITableViewScrollPositionBottom) animated:NO];
    }
    
}

#pragma mark -----懒加载--------
//懒加载messageDataArray

-(NSMutableArray *)messageDataArray{
    
    
    if (_messageDataArray == nil) {
        _messageDataArray = [[NSMutableArray alloc] init];
    }
    
    return _messageDataArray;
}




#pragma mark -----button事件-------

//开启语音通话

- (void)skxTurnToCallView{
    
    NSLog(@"跳转网络通话");
    [self cancelToolsView];
    
    SKXCallViewController *call = [[SKXCallViewController alloc] init];
    
    call.chater = self.chaterName;
    
    call.currentCall = self.currentCall;
    
    call.chaterBudy = self.chater;
    
    [self.navigationController pushViewController:call animated:YES];
    
    
}

//选择发送图片

-(void)skxChooseImageToSend{
    
    UIAlertAction *act = [UIAlertAction actionWithTitle:@"图库" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        
        
        NSLog(@"选择发送图片");
        
        //显示图片选择器
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
        
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        //设置源
        imgPicker.delegate = self;
        
        [self presentViewController:imgPicker animated:YES completion:^{
            
        }];
        
        
        
    }];
    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"相机" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
            
            UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"相机设备不可用" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [con addAction:act3];
            [self presentViewController:con animated:YES completion:nil];
            
        }else{
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.allowsEditing = YES;//设置可编辑
            
            
            [self presentViewController:picker animated:YES completion:^{//打开相机
                
            }];
        }
    }];
    
    UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"选择图片来源" preferredStyle:(UIAlertControllerStyleActionSheet)];
    
    [con addAction:act];
    [con addAction:act2];
    [self presentViewController:con animated:YES completion:nil];
    
    [self cancelToolsView];
    
}

//控制声音按钮
- (IBAction)skxChangeVoiceType:(UIButton *)sender {
    
    self.skxVoiceBtn.hidden = !self.skxVoiceBtn.hidden;
    
    [self.skxInputView resignFirstResponder];
    
    [self.skxInputView setText:nil];
    
}

//表情按钮
- (IBAction)skxChatFace:(UIButton *)sender {
}


//图片位置按钮
- (IBAction)skxChatMore:(UIButton *)sender {
    
    
    if (self.skxMoreToolView.hidden) {
        
        self.skxMoreToolView.hidden = NO;
        
        self.skxMoreToolView.sd_layout
        .heightIs(100);
        
        [self.skxInputView resignFirstResponder];
        
        self.skxToolsViewBottomSpace.constant = 100;
        
        self.skxChatTableViewBottom.constant = 150;
        
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.view layoutIfNeeded];
            
            
        }];
        
        
    }else{
        
        [self.skxInputView becomeFirstResponder];
        
        self.skxMoreToolView.sd_layout
        .heightIs(0);
        
        self.skxMoreToolView.hidden = YES;
        
        
    }
    
    
}


//开始录音
- (IBAction)skxStratRecordVoice:(UIButton *)sender {
    
    NSLog(@"开始录音");
        
    //文件名，以时间命名
    int x = arc4random() % 100000;
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSString *fileName = [NSString stringWithFormat:@"%d%d",(int)time , x];
    
    [[EMCDDeviceManager sharedInstance] asyncStartRecordingWithFileName:fileName completion:^(NSError *error) {
        if (!error) {
            NSLog(@"开始录音");
        }
    }];
    
    
    
    
    
    
}


//发送录音
- (IBAction)skxSendVoice:(UIButton *)sender {
    
    NSLog(@"结束录音并发送");
    
    
    
    [[EMCDDeviceManager sharedInstance] asyncStopRecordingWithCompletion:^(NSString *recordPath, NSInteger aDuration, NSError *error) {
        if (!error) {
            NSLog(@"录音成功");
            NSLog(@"%@" , recordPath);
            
            //发送语音给服务器
            [self sendVoice:recordPath Duration:aDuration];
        }
    }];
    
    
    
}

//发送语音
-(void)sendVoice:(NSString *)recordPath Duration:(NSInteger)duration{
    
    //1.构造一个语音的消息体
    EMChatVoice *chatVoice = [[EMChatVoice alloc] initWithFile:recordPath displayName:@"[语音]"];
    chatVoice.duration = duration;
    EMVoiceMessageBody *voiceBody = [[EMVoiceMessageBody alloc] initWithChatObject:chatVoice];
    voiceBody.duration = duration;
    
    [self sendMessageWithBody:voiceBody];
    
}


- (IBAction)skxCancelVoice:(UIButton *)sender {
    
    NSLog(@"取消发送");
    
    //    [[SKXFriendManager sharaFriendManager] cancelRecordVoice];
    
    [[EMCDDeviceManager sharedInstance] cancelCurrentRecording];
    
    
}



#pragma mark -----tableView代理事件-------

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if ([self.messageDataArray[indexPath.row]isKindOfClass:[NSString class]]) {
        
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tiCell"];
        
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
        cell.textLabel.text = self.messageDataArray[indexPath.row];
        
        return cell;
        
    }
    
    
    
    
    
    EMMessage *temp = self.messageDataArray[indexPath.row];
    
    NSString *userName = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    
    if([temp.from isEqualToString:userName]){
        
        //        SendTableViewCell *cell = [SendTableViewCell cellForTableView:tableView];
        
        SendTableViewCell *cell = (SendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"seCell"];
        if (!cell)
        {
            [tableView registerNib:[UINib nibWithNibName:@"SendTableViewCell" bundle:nil] forCellReuseIdentifier:@"seCell"];
            cell = [tableView dequeueReusableCellWithIdentifier:@"seCell"];
            
            
        }
        
        cell.message =temp;
        
        AVQuery *userQuery = [AVQuery queryWithClassName:@"_User"];
        
        [userQuery includeKey:@"username"];
        
        [userQuery whereKey:@"username" equalTo:userName];
        
        [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            
            AVObject *obj = [objects lastObject];
            
            cell.headerImageView.image = [UIImage imageNamed:obj[@"userImageName"]];
            
        }];

        
        
        self.cellHeight = [cell getCellHeight];
        
        return cell;
        
        
    }else{
        
        //        ReceiveTableViewCell *cell = [ReceiveTableViewCell cellForTableView:tableView];
        
        ReceiveTableViewCell *cell = (ReceiveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"reCell"];
        
        if (!cell)
        {
            [tableView registerNib:[UINib nibWithNibName:@"ReceiveTableViewCell" bundle:nil] forCellReuseIdentifier:@"reCell"];
            cell = [tableView dequeueReusableCellWithIdentifier:@"reCell"];
            
        }
        
        AVQuery *userQuery = [AVQuery queryWithClassName:@"_User"];
        
        [userQuery includeKey:@"username"];
        
        [userQuery whereKey:@"username" equalTo:temp.from];
        
        [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            
            AVObject *obj = [objects lastObject];
            
            cell.headerImageView.image = [UIImage imageNamed:obj[@"userImageName"]];
            
        }];

        
        cell.message = temp;
        
        self.cellHeight = [cell getCellHeight];
        
        return cell;
        
    }
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.messageDataArray.count;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.messageDataArray[indexPath.row] isKindOfClass:[NSString class]]) {
        return 20;
    }
    
    return self.cellHeight;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    NSLog(@"222222222");
    
    
    if (self.keyBStatus == NO) {
        
        self.skxToolsViewBottomSpace.constant = 0;
        
        self.skxChatTableViewBottom.constant = 50;
        
        [UIView animateWithDuration:0.25 animations:^{
            
            
            [self.view layoutIfNeeded];
            
        }];
        
        self.skxMoreToolView.hidden = YES;
        
    }else{
        
        self.skxMoreToolView.hidden = YES;
        
        [self.skxInputView resignFirstResponder];
        
    }
    
}


#pragma mark -----键盘的监听事件------


//键盘弹出时触发

-(void)KBWillShow:(NSNotification *)sender{
    
    //获取键盘高度
    CGRect kbFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat kbHeight = kbFrame.size.height;
    
    //底部提高
    
    self.skxToolsViewBottomSpace.constant = kbHeight;
    
    self.skxChatTableViewBottom.constant = kbHeight + 50;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.view layoutIfNeeded];
        
        
        
    }];
    
    self.keyBStatus = YES;
    self.skxMoreToolView.sd_layout
    .heightIs(0);
    self.skxMoreToolView.hidden = YES;
    
}


//键盘消失时触发

-(void)KBWillHide:(NSNotification *)sender{
    
    
    self.skxToolsViewBottomSpace.constant = 0;
    self.skxChatTableViewBottom.constant = 50;
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.view layoutIfNeeded];
        
        
        
    }];
    
    
    self.keyBStatus = NO;
    
}


-(void)cancelToolsView{
    
    self.skxMoreToolView.sd_layout
    .heightIs(0);
    self.skxMoreToolView.hidden = YES;
    self.skxToolsViewBottomSpace.constant = 0;
    self.skxChatTableViewBottom.constant = 50;
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.view layoutIfNeeded];
        
        
        
    }];
    
    
    
}


#pragma mark -----数据源操作--------

//获取本地聊天记录
-(void)loadMessageHistory{
    
    
    EMConversation * conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:self.chaterName conversationType:eConversationTypeChat];
    
    
    NSArray *tempArray = [conversation loadAllMessages];
    
//    self.messageDataArray = [NSMutableArray arrayWithArray:tempArray];
    
    
    for (EMMessage *message in tempArray) {
        [self addMessageDataArrayWithMessage:message];
    }

    
    NSLog(@"一共有%ld条数据记录",self.messageDataArray.count);
    
}

-(void)addMessageDataArrayWithMessage:(EMMessage *)message{
    
    //1.判断EMMessage对象前面是否要加时间
    NSString *timeString = [SKXFriendManager timeStr:message.timestamp];
    if (![self.currentTime isEqualToString:timeString]) {
        [self.messageDataArray addObject:timeString];
        
        self.currentTime = timeString;
    }
    
    //2.再加message对象
    [self.messageDataArray addObject:message];
    
    //设置消息为已读
    
    [self.currentCon markMessageWithId:message.messageId asRead:YES];
    
    
}
#pragma mark ------消息发送-------




//监听textview输入状态

-(void)textViewDidChange:(UITextView *)textView{
    
    //计算textView的高度，调整整个输入框的高度
    CGFloat textViewHeight = textView.contentSize.height;
    
    NSLog(@"%f",textViewHeight);
    
    if (textViewHeight < 35) {
        
    }else if (textViewHeight >68) {
        
        self.skxToolsViewHeight.constant = 68+10;
        
    }else{
        
        self.skxToolsViewHeight.constant = textViewHeight+10;
        
        
    }
    
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        
        [self loadViewIfNeeded];
        
    }];
    
    //判断最后一个字符是不是换行
    if([textView.text hasSuffix:@"\n"]){
        
        NSLog(@"发送消息");
        
        
        [self sendMessage:textView.text];
        
        //清空textview 的文字
        textView.text = nil;
        
        
        [textView setContentOffset:CGPointZero animated:YES];
        [textView scrollRangeToVisible:textView.selectedRange];
    }
    
}

-(void)sendMessage:(NSString *)text{
    
    
    text = [text substringToIndex:text.length-1];
    
    
    //创建文本对象
    
    EMChatText *chatText = [[EMChatText alloc] initWithText:text];
    
    //消息 = 消息头 + 消息体
    EMTextMessageBody *textbody = [[EMTextMessageBody alloc] initWithChatObject:chatText];
    
    //创建一个消息对像
    
    EMMessage *temp = [[EMMessage alloc] initWithReceiver:self.chaterName bodies:@[textbody]];
    
    temp.messageType = eMessageTypeChat;
    
    //发送消息
    [[EaseMob sharedInstance].chatManager asyncSendMessage:temp progress:nil prepare:^(EMMessage *message, EMError *error) {
        NSLog(@"准备发送消息");
    } onQueue:nil completion:^(EMMessage *message, EMError *error) {
        
        NSLog(@"完成消息发送%@",error);
        
    } onQueue:nil];
    
    
    self.skxToolsViewHeight.constant = 50;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self loadViewIfNeeded];
        
    }];
    
    [self addMessageDataArrayWithMessage:temp];
    [self.skxChatTableView reloadData];
    [self scrollToBottom];
    
    
}

#pragma mark -----接受好友到好友的消息--------

-(void)didReceiveMessage:(EMMessage *)message{
    
    //将接受的消息添加到数据源
    
    
    if ([[AVUser currentUser][@"voice"] isEqualToString:@"off"]) {
        
        [SKXFriendManager phoneVoice];

        
    }else{
        
        [SKXFriendManager phoneVoice];
        
    }
    
    if ([[AVUser currentUser][@"shock"] isEqualToString:@"off"]) {
        
        [SKXFriendManager phoneVibrate];

        
    }else{
        
        [SKXFriendManager phoneVibrate];
    }

    
    if ([message.from isEqualToString:self.chaterName]) {
        
        [self addMessageDataArrayWithMessage:message];
        
        [self.skxChatTableView reloadData];
        
        [self scrollToBottom];
        
    }
    
}

// //显示最新消息在最上面

-(void)scrollToBottom{
    
    //获取最后一行
    
    if (self.messageDataArray.count==0) {
        
        
        
        NSLog(@"数据尚未加载");
        
        
    }else{
        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messageDataArray.count -1 inSection:0];
        
        
        
        [self.skxChatTableView scrollToRowAtIndexPath:lastIndex atScrollPosition:(UITableViewScrollPositionBottom) animated:YES];
        
        
        NSLog(@"数据加载，最后一行%@",lastIndex);
    }
    
}

#pragma mark -------选择图片的代理方法--------

//用户选中图片的回调


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *selectedImg = info[UIImagePickerControllerOriginalImage];
    
    //发送图片
    
    [self sendImageMessageWith:selectedImg];
    
    //隐藏图片选择控制器
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

-(void)sendImageMessageWith:(UIImage *)image{
    
    EMChatImage *chatImg = [[EMChatImage alloc] initWithUIImage:image displayName:@"【图片】"];
    
    
    EMImageMessageBody *imgBody = [[EMImageMessageBody alloc] initWithImage:chatImg thumbnailImage:nil];
    
    
    [self sendMessageWithBody:imgBody];
    
}


-(void)sendMessageWithBody:(id<IEMMessageBody>)body{
    
    
    EMMessage *message = [[EMMessage alloc] initWithReceiver:self.chaterName bodies:@[body]];
    
    message.messageType = eMessageTypeChat;
    
    [[EaseMob sharedInstance].chatManager asyncSendMessage:message progress:nil prepare:^(EMMessage *message, EMError *error) {
        
    } onQueue:nil completion:^(EMMessage *message, EMError *error) {
        if(error){
            
            NSLog(@"%@",error);
            
        }
        
    } onQueue:nil];
    
    [self addMessageDataArrayWithMessage:message];
    [self.skxChatTableView reloadData];
    [self scrollToBottom];
       
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    
    [SKXFriendManager stop];
    
}



@end
